package com.eco.fanliapp.controller.app;

import com.eco.fanliapp.common.base.AuthController;
import com.eco.fanliapp.common.constant.VariableName;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.dao.BannerPictureMapper;
import com.eco.fanliapp.entity.AppUser;
import com.eco.fanliapp.entity.BannerPicture;
import com.eco.fanliapp.service.IBannerPictureService;
import com.qiniu.util.Auth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @ProjectName: fanliapp
 * @Package: com.eco.fanliapp.controller.app
 * @ClassName: AdvertisingImage
 * @Author: LC
 * @Description: 图片与邀请赚钱海报
 * @Date: 2019/11/28 15:13
 * @Version: 4.2.2
 */
@RestController
@RequestMapping("/mobile/img")
public class AdvertisingImageController extends AuthController {

	@Autowired
	private IBannerPictureService bannerPictureService;

	@Autowired
	private BannerPictureMapper pictureMapper;

	/**
	 * 广告位图片
	 *
	 * @return
	 */
	@GetMapping("/getPoster")
	public ServerResponse<String> getPoster() {
		BannerPicture picture = new BannerPicture();
//		picture.setCompanyId(getAppUser().getCompanyId());
		picture.setPictureStatus(2);
		picture.setPicType(1);
		BannerPicture picture2 = pictureMapper.findAll(picture).get(0);
		if (picture2 != null) {
			return ServerResponse.createBySuccess("数据获取成功", picture2.getMainPictureUrl());
		} else {
			return ServerResponse.createBySuccessCodeMessage(201, "暂无数据");
		}
	}

	/**
	 * 邀请链接-海报生成
	 *
	 * @return
	 */
	@GetMapping("/getInvitationPoster")
	public ServerResponse<Map<String, Object>> getInvitationPoster() {
		AppUser appUser = getAppUser();
		return bannerPictureService.selectByBannerPictureInvitationPoster(
				appUser == null ? null : getAppUser().getUserId(),
				appUser == null ? null :getAppUser().getCompanyId());
	}

	/**
	 * 邀请赚钱链接-新
	 *
	 * @return
	 */
	@GetMapping("/getInvitationPosterUrlNew")
	public ServerResponse<Map<String, Object>> getInvitationPosterUrlNew() {
		AppUser appUser = getAppUser();
		return bannerPictureService.selectByBannerPictureUrlNew(appUser == null ? null : getAppUser().getUserId());
	}

	/**
	 * 邀请赚钱链接-旧
	 *
	 * @return
	 */
	@GetMapping("/getInvitationPosterUrl")
	public ServerResponse<Map<String, Object>> getInvitationPosterUrl() {
		AppUser appUser = getAppUser();
		return bannerPictureService.selectByBannerPictureUrl(appUser == null ? null : getAppUser().getUserId());
	}

	/**
	 * 启动页图
	 *
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/findByPicTypeThree")
	public ServerResponse<List<BannerPicture>> findByPicTypeThree() {
		BannerPicture picture = new BannerPicture();
		picture.setPicType(3);
		picture.setPictureStatus(2);
		//公司
//		picture.setCompanyId(getAppUser().getCompanyId());
		List<BannerPicture> list = pictureMapper.findAll(picture);
		if (list.size() > 0) {
			return ServerResponse.createBySuccess("数据获取成功", list);
		} else {
			return ServerResponse.createBySuccess(201, "暂无数据", list);
		}
	}

	/**
	 * 首页图
	 *
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/findByPicTypeFour")
	public ServerResponse<List<BannerPicture>> findByPicTypeFour() {
		BannerPicture picture = new BannerPicture();
		picture.setPicType(4);
		picture.setPictureStatus(2);
//		picture.setCompanyId(getAppUser().getCompanyId());
		List<BannerPicture> list = pictureMapper.findHomePageBannerPicture();
		if (list.size() > 0) {
			return ServerResponse.createBySuccess("数据获取成功", list);
		} else {
			return ServerResponse.createBySuccess(201, "暂无数据", list);
		}
	}

	/**
	 * 获取上传七牛云图片token
	 *
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/getTokenByQiNiuYun")
	public ServerResponse<String> getTokenByQiNiuYun() {
		//AccessKey的值
		String accessKey = VariableName.accessKey;
		//SecretKey的值
		String secretKey = VariableName.secretKey;
		//存储空间名
		String bucket = VariableName.bucket;
		Auth auth = Auth.create(accessKey, secretKey);
		String upToken = auth.uploadToken(bucket);

		return ServerResponse.createBySuccess("获取成功", upToken);
	}

	/**
	 * 广告位弹窗
	 *
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/advertisingPopup")
	public ServerResponse advertisingPopup() {
		AppUser appUser = getAppUser();
		if (appUser == null) {
			return ServerResponse.createByErrorCodeMessage(301, "token不正确");
		}
		return bannerPictureService.advertisingPopup(appUser.getUserId());
	}

	/**
	 * 首页海报
	 *
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/homePosterList")
	public ServerResponse homePosterList() {
		AppUser appUser = getAppUser();
		if (appUser == null) {
			return bannerPictureService.homePosterList(null);
		}
		return bannerPictureService.homePosterList(appUser.getUserId());
	}
}
